package com.guw.AWT;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestField1 {
    public static void main(String[] args) {
        new MyFrame1();
    }
}

class MyFrame1 extends Frame{
    public MyFrame1(){
        //创建文本框
        TextField tf = new TextField();
        add(tf);

        //监听这个文本框输入的文字
        MyActionLister ma = new MyActionLister();
        //按下enter就会触发这个输入框的事件
        tf.addActionListener(ma);

        //设置替换编码,输入框的文字被替换了
        tf.setEchoChar('*');

        setVisible(true);
        pack();

    }

    private static void windowClose(Frame f){
        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}

class MyActionLister implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //获得一些资源,返回一个对象,强转成TextField类型
        TextField tf = (TextField)e.getSource();
        //获得输入框的文本
        System.out.println(tf.getText());
        //清空对话框
        tf.setText("");
    }
}

